package com.xyhuo.leetcode.december;

/**
 * 编辑距离
 * https://leetcode.cn/problems/edit-distance/
 *
 * @desc 看的题解，没有头绪
 * @time 2023/12/27
 */
public class MinDistance {

    public int minDistance(String word1, String word2) {
        char[] word1CharArray = word1.toCharArray();
        char[] word2CharArray = word2.toCharArray();
        int m = word1CharArray.length;
        int n = word2CharArray.length;
        int[][] dp = new int[m + 1][n + 1];
        for (int i = 0; i <= n; i++) {
            dp[0][i] = i;
        }
        for (int i = 0; i <= m; i++) {
            dp[i][0] = i;
        }

        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                if (word1CharArray[i] == word2CharArray[j]) {
                    dp[i][j] = dp[i - 1][j - 1];
                } else {
                    dp[i][j] = Math.min(Math.min(dp[i - 1][j], dp[i - 1][j - 1]), dp[i][j - 1]) + 1;
                }
            }
        }
        return dp[m][n];
    }
}
